package com.mybase.app.fourBase;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.mybase.app.R;

import java.io.File;

/**
 * Created by happy_000 on 2014/10/17.
 */
public class CameraActivity extends Activity {
    private ImageView imgView;
    private Uri outputFileUri;
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.camera);
        imgView= (ImageView) findViewById(R.id.camera_img);
        textView= (TextView) findViewById(R.id.camera_tv);
        Button button= (Button) findViewById(R.id.camera_btn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                File file =new File(Environment.getExternalStorageDirectory(),"test.jpg");// 存储图片的目标文件
                outputFileUri=Uri.fromFile(file);
                Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT,outputFileUri);
                startActivityForResult(intent,1);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        StringBuffer string=new StringBuffer("图片地址:");
            if (data!=null){
                Bitmap bitmap=data.getParcelableExtra("data");
                imgView.setImageBitmap(bitmap);
                string=string.append("data!=null**");
            }else {
                BitmapFactory.Options  bitmapFactory=new BitmapFactory.Options();
                bitmapFactory.inJustDecodeBounds=true;
                BitmapFactory.decodeFile(outputFileUri.getPath(),bitmapFactory);
                Bitmap bitmap=BitmapFactory.decodeFile(outputFileUri.getPath(),bitmapFactory);
                imgView.setImageBitmap(bitmap);
                string=string.append("data==null**");
        }
        string=string.append(outputFileUri.getPath());
        textView.setText(string);
    }
}
